package com.yy.main.app.web;

import com.yy.constant.AppConstant;
import com.yy.kit.log.YYLogKits;
import com.yy.main.app.service.IUserService;
import com.yy.main.app.web.base.CommonController;
import com.yy.pojo.base.TUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * <p>
 * 用户登陆控制层
 * </p>
 *
 * @author 杨阳
 * @since 2017年10月25日
 */
@Controller
@Api(value = "用户登陆Controller", description = "登入，登出等一些相关操作", tags = {"用户登陆，注销controller"})
public class LoginController extends CommonController {

    @Resource(name = "iUserService")
    private IUserService iUserService;


    /**
     * <p>
     * 跳转到登陆页
     * </p>
     *
     * @return {@link ModelAndView}
     * @author 杨阳
     * @since 2017年8月30日15:29:21
     */
    @ApiOperation(value = "跳转到登陆页面", httpMethod = "GET")
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView login() {
        return goToPageByModelAndView("login", null);
    }

    /**
     * <p>
     * 用户登陆
     * </p>
     *
     * @return {@link java.util.Map}
     * @author 杨阳
     * @since 2017年8月30日15:29:21
     */
    @RequestMapping("/login")
    @ResponseBody
    @ApiOperation(value = "用户登陆操作", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "用户帐号", required = true, paramType = "form"),
            @ApiImplicitParam(name = "password", value = "用户密码", required = true, paramType = "form")
    })
    public Map<String, Object> doLogin(TUser user, HttpSession session) {
        Map<String, Object> result = iUserService.userLogin(user.getAccount(), user.getPassword(), session);
        return result;
    }

    /**
     * <p>
     * 跳转到管理端首页
     * </p>
     *
     * @return {@link ModelAndView}
     * @author 杨阳
     * @since 2017年8月30日15:29:21
     */
    @ApiOperation(value = "跳转到管理端", httpMethod = "GET")
    @RequestMapping(value = "/system", method = RequestMethod.GET)
    public ModelAndView goSystemNav() {
        return goToPageByModelAndView("system/system_nav", null);
    }

    /**
     * <p>
     * 退出登陆
     * </p>
     *
     * @return redirect:/登陆页
     * @author 杨阳
     * @since 2017年9月4日17:13:413
     */
    @ApiOperation(value = "退出登陆", httpMethod = "GET", notes = "注销用户session")
    @RequestMapping("/logout")
    public String logOut(HttpSession session) {
        if (null != session && null != session.getAttribute(AppConstant.SESSION_KEY)) {
            TUser user = (TUser) session.getAttribute(AppConstant.SESSION_KEY);
            YYLogKits.info(LoginController.class, "┌───────────────────────────────────────────────────────────────────────────┐");
            YYLogKits.info(LoginController.class, "├用户：{}注销登陆!", user.getName());
            YYLogKits.info(LoginController.class, "└───────────────────────────────────────────────────────────────────────────┘");
            session.invalidate();
        }
        return "redirect:/";
    }
}
